package ru.dc.gui.swing;

import ru.dc.Application;
import ru.dc.object.ServerInfo;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 * User: Penkov Vladimir
 * Date: 01.11.2010
 * Time: 12:14:40
 * </pre>
 */
public class ConnectionsTableModel extends AbstractTableModel {

    private String[] columnNames = {"Адрес", "Кодировка"};
    private List<ServerInfo> connections = new ArrayList<ServerInfo>();

    public void refresh() {
        connections.clear();
        for (ServerInfo serverInfo : Application.getApplication().getConnectionManager().enumerateHubConnections()) {
            connections.add(serverInfo);
        }
    }

    public int getRowCount() {
        return connections.size();
    }

    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        final ServerInfo serverInfo = connections.get(rowIndex);
        switch (columnIndex) {
            case 0 : return serverInfo.getAddress()+":"+serverInfo.getPort();
            case 1 : return serverInfo.getEncoding();
        }
        return null;
    }

    public List<ServerInfo> getConnections() {
        return connections;
    }
}
